/**
 * 
 */
package com.Data;


import java.io.Serializable;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.SoapClasses.SoapAlbums;
import com.shareup.R;
import com.utils.AlertDialogHelper;


/**
 * @author Chris
 * 
 */
public class Albums extends AsyncTask<String, String, Albums[]> implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2733723568667257417L;

    public void setMode(int mode) {

        this.Mode = mode;
    }

    public static final int ACTION_ADD = 224;

    public static final int ACTION_DELETE = 554;

    public static final int ACTION_GET = 656;

    public static final int ACTION_UPDATE = 223;

    public static final int ACTION_UPDATE_COVER = 226;

    private onAddCompleted callbackAdd;

    private onDeleteCompleted callbackDelete;

    private onGetCompleted callbackGet;

    private onUpdateCompleted callbackUpdate;

    private Context context;

    private int Count;

    private String Cover;

    private String Date;

    private int ID;

    private int Mode;

    private String Name;

    private User Owner;

    private int Pin;

    private int Privacy;

    private AlertDialog progress;

    private boolean singleAlbum;

    public interface onAddCompleted {

        void Completed(String result);
    }

    public interface onDeleteCompleted {

        void Completed(String result);
    }

    public interface onGetCompleted {

        void Completed(Albums[] result);
    }

    public interface onUpdateCompleted {

        void Completed(String result);
    }

    public Albums() {

    }

    public Albums(int mode) {

        super();
        this.Mode = mode;

    }

    public int getCount() {

        return this.Count;
    }

    public String getCover() {

        return Cover;
    }

    public String getDate() {

        return this.Date;
    }

    public int getID() {

        return this.ID;
    }

    public String getName() {

        return this.Name;
    }

    public User getOwner() {

        return this.Owner;
    }

    public int getPin() {

        return this.Pin;
    }

    public int getPrivacy() {

        return this.Privacy;
    }

    public boolean isSingleAlbum() {

        return this.singleAlbum;
    }

    public void setCallbackAdd(onAddCompleted callbackAdd) {

        this.callbackAdd = callbackAdd;
    }

    public void setCallbackDelete(onDeleteCompleted callbackDelete) {

        this.callbackDelete = callbackDelete;
    }

    public void setCallbackGet(onGetCompleted callbackGet) {

        this.callbackGet = callbackGet;
    }

    public void setCallbackUpdate(onUpdateCompleted callbackUpdate) {

        this.callbackUpdate = callbackUpdate;
    }

    public void setContext(Context context) {

        this.context = context;
    }

    public void setCount(int count) {

        this.Count = count;
    }

    public void setCover(String cover) {

        this.Cover = cover;
    }

    public void setDate(String date) {

        this.Date = date;
    }

    public void setID(int iD) {

        this.ID = iD;
    }

    public void setName(String name) {

        this.Name = name;
    }

    public void setOwner(User owner) {

        this.Owner = owner;
    }

    public void setPin(int pin) {

        this.Pin = pin;
    }

    public void setPrivacy(int privacy) {

        this.Privacy = privacy;
    }

    public void setSingleAlbum(boolean singleAlbum) {

        this.singleAlbum = singleAlbum;
    }

    /*
     * (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
     */
    @Override
    protected Albums[] doInBackground(String... params) {

        SoapAlbums sal = new SoapAlbums();
        Albums[] als;

        String id = String.valueOf(this.ID);
        String idOwner = "";
        if (this.Owner != null)
            idOwner = String.valueOf(this.Owner.getID());
        String pin = String.valueOf(this.Pin);
        String priv = String.valueOf(this.Privacy);

        Log.e("SENDING -PIN", pin);

        if (this.Cover == null || this.Cover.equals(""))
            this.Cover = "0";
        switch (Mode) {
            case ACTION_GET:
                return sal.getAlbum(idOwner, this.singleAlbum);
            case ACTION_DELETE:
                als = new Albums[1];
                this.Name = sal.deleteAlbum(new String[] { id,
                            idOwner });
                als[0] = this;
                return als;
            case ACTION_UPDATE:
                als = new Albums[1];
                this.Name = sal.updateAlbum(new String[] { id,
                            this.Name, pin, this.Cover, priv });
                als[0] = this;
                return als;
            case ACTION_UPDATE_COVER:
                als = new Albums[1];
                this.Name = sal.updateAlbumCover(new String[] { id,
                            this.Cover });
                als[0] = this;
                return als;
            case ACTION_ADD:
                als = new Albums[1];
                this.Name = sal.addAlbum(new String[] { this.Name,
                            pin, idOwner, this.Cover, priv });
                als[0] = this;
                return als;
            default:
                return null;
        }

    }

    @Override
    protected void onPostExecute(Albums[] result) {

        if (callbackGet != null)
            callbackGet.Completed(result);

        if (callbackDelete != null)
            callbackDelete.Completed(result[0].getName());

        if (callbackUpdate != null) {
            callbackUpdate.Completed(result[0].getName());
            progress.dismiss();
        }

        if (callbackAdd != null) {
            progress.dismiss();
            callbackAdd.Completed(result[0].getName());
        }

    }

    @Override
    protected void onPreExecute() {

        if (callbackUpdate != null) {
            String mess = context.getResources().getString(R.string.dialog_saving);
            progress = AlertDialogHelper.setProgressDialog(progress, mess, context);
            progress.show();
        }
        if (callbackAdd != null) {
            String mess = context.getResources().getString(R.string.dialog_creating_album);
            progress = AlertDialogHelper.setProgressDialog(progress, mess, context);
            progress.show();
        }
    }

}
